package cae.gui.util;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.Popup;
import javax.swing.PopupFactory;

public class CaePopupUtil
{
	private Component parent;
	private String titulo;
	private String mensaje;

	public CaePopupUtil(Component parent, String titulo, String mensaje) 
	{
		this.parent = parent;
		this.titulo = titulo;
		this.mensaje = mensaje;
	}

	public void showNow()
	{
		PopupFactory factory = PopupFactory.getSharedInstance();
		
		JPanel component = new JPanel(new GridBagLayout());
		component.setBackground(Color.YELLOW);
		
		GridBagConstraints destConst = new GridBagConstraints();
		destConst.insets = new Insets(10,10,10,10);
		destConst.fill = GridBagConstraints.BOTH;
		
		destConst.gridx = 0;
		destConst.gridy = 0;
		destConst.weightx = 100;
		JLabel title = new JLabel(titulo);
		component.add(title, destConst);
		
		destConst.gridx = 1;
		destConst.gridy = 0;
		destConst.weightx = 0;
		JButton close = new JButton(CaeImage.getImageIcon("img/close_icon.gif"));
		close.setPreferredSize(new Dimension(16, 16));
		component.add(close, destConst);
		
		
		destConst.weightx = 100;
		destConst.weighty = 100;
		destConst.gridx = 0;
		destConst.gridy = 1;
		destConst.gridwidth = 2;
		JTextArea text = new JTextArea(mensaje);
		text.setEditable(true);
		text.setEnabled(true);
		component.add(text, destConst);
		component.setPreferredSize(new Dimension(300, 300));
		//component.setAlwaysOnTop(true);

		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();


		int x = (int) ((screenSize.width - 300)/2); 
		int y = (int) ((screenSize.height - 300 )/2);

		final Popup popup = factory.getPopup(parent, component, x, y);
		System.out.println("shows");
		popup.show();
		
		ActionListener hider = new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				popup.hide();
			}
		};
		
		close.addActionListener(hider);
		// Hide popup in 3 seconds
		//Timer timer = new Timer(10000, hider);
		//timer.start();
	}
}